﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;

namespace Web.Areas.Manager.Controllers
{

    [UserAuthorize]
    public partial class Sys_UserController : Controller
    {
        Sys_Enterprise_BLL _Sys_Enterprise_BLL = new Sys_Enterprise_BLL();
        Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();
        Sys_Role_BLL _Sys_Role_BLL = new Sys_Role_BLL();
        Sys_UsersInRoles_BLL _Sys_UsersInRoles_BLL = new Sys_UsersInRoles_BLL();
        Sys_Theme_BLL _Sys_Theme_BLL = new Sys_Theme_BLL();
        Sys_Organizations_BLL _Sys_Organizations_BLL = new Sys_Organizations_BLL();


        public virtual ActionResult Index(int? pageIndex, string keyword)
        {
            var model = _Sys_Users_BLL.Get().Where(a=>a.Sys_Enterprise.Sys_Users.Any(b => b.Id.Equals(new Guid(User.Identity.Name))));

            if (!string.IsNullOrEmpty(keyword))
            {
                model = model.Where(a => a.UserName.Contains(keyword));
            }
            return View(model.ToPagedList(pageIndex));
        }

        public virtual ActionResult Details(Guid Id)
        {
            ViewData.Add("Sys_Enterprise", _Sys_Enterprise_BLL.Get().Where(a => a.Sys_Users.Any(b => b.Id.Equals(new Guid(User.Identity.Name)))));
            ViewData.Add("Sys_Roles", _Sys_Role_BLL.Get().Where(a => a.Global || a.Sys_Enterprise.Sys_Users.Any(b => b.Id.Equals(new Guid(User.Identity.Name)))));
            ViewData.Add("Sys_Theme", _Sys_Theme_BLL.Get());
            ViewData.Add("Sys_Organizations", _Sys_Organizations_BLL.Get().Where(a => a.Sys_Enterprise.Sys_Users.Any(b => b.Id.Equals(new Guid(User.Identity.Name)))));

            var item = _Sys_Users_BLL.Get(Id);
            return View(item);
        }

        public virtual ActionResult Edit(Guid? id)
        {
            var item = _Sys_Users_BLL.Get(id);

            ViewData.Add("Sys_Enterprise", _Sys_Enterprise_BLL.Get().Where(a=>a.Sys_Users.Any(b=>b.Id.Equals(new Guid(User.Identity.Name)))));
            ViewData.Add("Sys_Roles", _Sys_Role_BLL.Get().Where(a=>a.Global || a.Sys_Enterprise.Sys_Users.Any(b=>b.Id.Equals(new Guid(User.Identity.Name)))));
            ViewData.Add("Sys_Theme", _Sys_Theme_BLL.Get());
            ViewData.Add("Sys_Organizations", _Sys_Organizations_BLL.Get().Where(a => a.Sys_Enterprise.Sys_Users.Any(b => b.Id.Equals(new Guid(User.Identity.Name)))));

            return View(item);
        }

        [HttpPost]
        public virtual ActionResult Edit(Guid? id, Sys_Users collection)
        {
            //检查该用户是否存在

            if (!id.HasValue && _Sys_Users_BLL.Get(collection.UserName, collection.EnterpriseId) != null)
            {
                ModelState.AddModelError("UserName", "该用户已存在");
            }

            if (!ModelState.IsValid)
            {
                ViewData.Add("Sys_Enterprise", _Sys_Enterprise_BLL.Get().Where(a => a.Sys_Users.Any(b => b.Id.Equals(new Guid(User.Identity.Name)))));
                ViewData.Add("Sys_Roles", _Sys_Role_BLL.Get().Where(a => a.Global || a.Sys_Enterprise.Sys_Users.Any(b => b.Id.Equals(new Guid(User.Identity.Name)))));
                ViewData.Add("Sys_Theme", _Sys_Theme_BLL.Get());
                ViewData.Add("Sys_Organizations", _Sys_Organizations_BLL.Get().Where(a => a.Sys_Enterprise.Sys_Users.Any(b => b.Id.Equals(new Guid(User.Identity.Name)))));
                return View(collection);
            }

            _Sys_Users_BLL.Save(id, ref collection);

            //保存用户所属角色

            foreach (var item in _Sys_Role_BLL.Get())
            {
                _Sys_UsersInRoles_BLL.Delete(collection.Id, item.Id);

                if (Request[item.Id.ToString()] == "true,false")
                {
                    _Sys_UsersInRoles_BLL.Insert(collection.Id, item.Id);
                }
            }

            return RedirectToAction("Index");
        }

        [HttpDelete]
        public virtual ActionResult Delete(Guid Id)
        {
            _Sys_Users_BLL.DeleteObject(Id);
            return RedirectToAction("Index");
        }
    }
}
